package com.hp.tiger.concurrent.classloader.chapter1;

import java.util.Random;

/**
 * @author XavierDong
 **/
public class ClassActiveUse {

    static {
        System.out.println("ClassActiveUse");
    }

    public static void main(String[] args) throws ClassNotFoundException {
//        new Obj();
//        System.out.println(I.a);
//        System.out.println(Obj.salary);
//        Obj.printSalary();


//        Class.forName("com.hp.tiger.concurrent.classloader.chapter1.Obj");

//        System.out.println(Child.age);

//        子类不初始化，父类初始化，通过子类访问父类的静态变量，不会导致子类的初始化
//        System.out.println(Child.salary);

        //不会初始化，定义引用数组，不会初始化
//        Obj[] objs = new Obj[10];

//        不会初始化,final常量会在编译期间放入常量池，不会初始化类
//        System.out.println(Obj.salary);
//        会初始化 final修饰的复杂类型，在编译期间无法计算得出，会初始化类
//        System.out.println(Obj.a);
    }
}

class Obj {

    public static final long salary = 100000L;
    public static final int a = new Random().nextInt(100);

    public static void printSalary() {
        System.out.println("===========Obj=====" + salary);
    }

    static {
        System.out.println("Obj 被初始化.");
    }
}

class Child extends Obj {

    public static int age = 30;

    static {
        System.out.println("Child 被初始化.");
    }
}


interface I {
    int a = 10;
}
